class_name Player
extends Unit

signal on_player_died

@export var dash_duration := 0.5
@export var dash_speed_multi := 2.5
@export var dash_cooldown := 0.5

@onready var collision_shape_2d: CollisionShape2D = $CollisionShape2D
@onready var dash_timer: Timer = $DashTimer
@onready var dash_cooldown_timer: Timer = $DashCooldownTimer
@onready var trail: Trail = %Trail
@onready var weapon_container: WeaponContainer = %WeaponContainer

var current_weapons: Array[Weapon] = []
var move_dir: Vector2
var is_dashing := false
var dash_available := true

func _ready() -> void:
	super._ready()
	dash_timer.wait_time = dash_duration
	dash_cooldown_timer.wait_time = dash_cooldown


func _process(delta: float) -> void:
	if Global.game_paused: return
	move_dir = Input.get_vector("move_left", "move_right", "move_up", "move_down")
	var current_velocity := move_dir * stats.speed
	if is_dashing:
		current_velocity *= dash_speed_multi
	position += current_velocity * delta
	position.x = clamp(position.x, -1000, 1000)
	position.y = clamp(position.y, -500, 500)
	
	if can_dash():
		start_dash()
	
	update_animation()
	update_rotation()
	
func add_weapon(data: ItemWeapon) -> void:
	var weapon := data.scene.instantiate() as Weapon
	add_child(weapon)
	weapon.setup_weapon(data)
	current_weapons.append(weapon)
	
	weapon_container.update_weapons_position(current_weapons)
	
	Global.equipped_weapons.append(data)
	

func del_weapon(weapon: Weapon) -> void:
	current_weapons.erase(weapon)
	Global.equipped_weapons.erase(weapon.data)
	
func update_animation() -> void:
	if move_dir.length() > 0:
		animation_player.play("move")
	else:
		animation_player.play("idle")
		
func update_rotation() -> void:
	if move_dir == Vector2.ZERO:
		return
	if move_dir.x >= 0.1:
		visuals.scale = Vector2(-0.5, 0.5)
	else:
		visuals.scale = Vector2(0.5, 0.5)

func can_dash() -> bool:
	return not is_dashing and move_dir != Vector2.ZERO and dash_cooldown_timer.is_stopped() and Input.is_action_just_pressed("dash")
	
	
func is_facing_right() -> bool:
	return visuals.scale.x == -0.5

func start_dash() -> void:
	is_dashing = true
	visuals.modulate.a = 0.5
	dash_timer.start()
	trail.start_trail()
	collision_shape_2d.set_deferred("disabled", true)
	
func update_player_new_wave() -> void:
	stats.health += stats.health_increase_per_wave
	health_component.setup(stats)
	
func heal(amount: float) -> void:
	health_component.heal(amount)
	Global.create_heal_text(self, amount)


func _on_dash_timer_timeout() -> void:
	is_dashing = false
	move_dir = Vector2.ZERO
	visuals.modulate.a = 1
	dash_cooldown_timer.start()
	collision_shape_2d.set_deferred("disabled", false)


func _on_hp_regen_timer_timeout() -> void:
	if health_component.is_died():
		return
	if health_component.current_health < stats.health and stats.hp_regen != 0:
		heal(stats.hp_regen)
		
func _on_hurtbox_component_on_damage(hitbox: HitboxComponent) -> void:
	super._on_hurtbox_component_on_damage(hitbox)
	if health_component.is_died():
		on_player_died.emit()
